package com.orangelife.mobile.washcar.biz;

import com.curry.android.http.OrangeDataHandler;
import com.curry.android.http.OrangeErrorHandler;
import com.curry.android.http.VolleyHttpUtil;
import com.google.gson.reflect.TypeToken;
import com.orangelife.mobile.constants.ConfirmOrderURL;
import com.orangelife.mobile.constants.WashCarURL;
import com.orangelife.mobile.washcar.entity.WashCarChooseEntity;
import com.orangelife.mobile.washcar.entity.WashCarDetailEntity;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 洗车的http请求
 * Created by ${shz} on 2016/1/19.
 */
public class WashCarBiz {

    private static WashCarBiz washCar = null;


    /**
     * 单例
     */
    public static WashCarBiz getInstance() {
        if (washCar == null) {
            washCar = new WashCarBiz();
        }
        return washCar;
    }

    /**
     * 请求洗车的规格
     */
    public void getWashCarChoose(HashMap<String, String> map, VolleyHttpUtil.ResponseCallBack callBack) {
        VolleyHttpUtil.getInstance().getWithCallback(WashCarURL.URL_CLEAN_CAR_CHOOSE, map, callBack);
    }

    /**
     * 获取洗车的规格
     *
     * @param response
     * @return
     */
    public static List<WashCarChooseEntity> handleWashCarChoose(JSONObject response, int errCode) {
        List<WashCarChooseEntity> list = new ArrayList<>();
        if (OrangeErrorHandler.getInstance().isSuccess(response)) {
            list = OrangeDataHandler.getListFromJsonWithPageEntity(
                    response, new TypeToken<List<WashCarChooseEntity>>() {
                    }.getType(), null, null);
        }
        return list;
    }

    /**
     * 添加dd洗车订单
     */
    public void addWashCarOrder(HashMap<String, Object> map, VolleyHttpUtil.ResponseCallBack callBack) {
        VolleyHttpUtil.getInstance().postWithCallback(ConfirmOrderURL.ADD_CLEAN_CAR, map, callBack);
    }

    /**
     * 请求dd洗车订单详情
     */
    public void getWashCarDetail(HashMap<String, String> map, VolleyHttpUtil.ResponseCallBack callBack) {
        VolleyHttpUtil.getInstance().getWithCallback(WashCarURL.URL_CLEAN_CAR_DETAILS, map, callBack);
    }

    /**
     * 获取dd洗车的订单详情
     *
     * @param response
     * @return
     */
    public static WashCarDetailEntity handleWashCarDetail(JSONObject response, int errCode) {
        WashCarDetailEntity list = new WashCarDetailEntity();
        if (OrangeErrorHandler.getInstance().isSuccess(response)) {
            list = (WashCarDetailEntity) OrangeDataHandler.getBeanWithEntity(response, WashCarDetailEntity.class);
        }
        return list;
    }

    /**
     * 获取dd洗车的订单跟踪
     *
     * @param response
     * @return
     */
    public static List<Map<String, Object>> handleWashCarDetailFollow(JSONObject response, int errCode) {
        List<Map<String, Object>> list = new ArrayList<>();
        if (OrangeErrorHandler.getInstance().isSuccess(response)) {
            list = OrangeDataHandler.getListFromJsonWithPageEntity(
                    response, new TypeToken<List<Map<String, Object>>>() {
                    }.getType(), null, null);
        }
        return list;
    }


    /**
     * 改变dd洗车的订单状态（结算）
     *
     * @param map
     * @param callBack
     */
    public void putChangeWashCarStatus(HashMap<String, Object> map, VolleyHttpUtil.ResponseCallBack callBack) {
        VolleyHttpUtil.getInstance().putWithCallback(WashCarURL.URL_CLEAN_CAR_STATUE, map, callBack);
    }

    /**
     * 改变dd洗车的订单状态（结算）
     */
    public void putCancleWashCarOrder(String orderID, VolleyHttpUtil.ResponseCallBack callBack) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("orderID", orderID);
        map.put("remark", "");
        VolleyHttpUtil.getInstance().putWithCallback(WashCarURL.URL_CANCLE_CLEAN_CAR, map, callBack);
    }

    /**
     * 请求dd洗车的列表
     *
     * @param map
     * @param
     */
    public void requestCarOrdersList(HashMap<String, String> map, VolleyHttpUtil.ResponseCallBack callBack) {
        VolleyHttpUtil.getInstance().getWithCallback(WashCarURL.URL_MY_CAR_ORDER_LIST, map, callBack);
    }


    /**
     * 判断传入的字符串是否是NULL或者是空字符串，或者是否是NULL字符串
     *
     * @param str
     * @return
     */

    public static boolean isNullOrEmpty(String str) {
        if (null == str || "".equals(str.trim())) {
            return true;
        }
        return false;
    }

    /**
     * 地址拼接
     *
     * @param map
     * @return
     */
    private String assemblyRestUrl(String url, Map<String, Object> map) {
        String uri = url;
        if (map != null) {

            Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator();

            while (it.hasNext()) {
                Map.Entry<String, Object> entry = it.next();
                if (entry.getKey().indexOf("{") > -1 || entry.getKey().indexOf("}") > -1) {
                    uri = uri.replace(entry.getKey(), entry.getValue().toString());
                }
            }

            return uri;
        } else {
            return uri;
        }

    }

    /**
     * 地址拼接2
     *
     * @param map
     * @return
     */
    private String assemblyUrl(Map<String, Object> map) {
        if (map != null) {
            StringBuffer sb_url = new StringBuffer();
            Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator();

            while (it.hasNext()) {
                Map.Entry<String, Object> entry = it.next();
                if (entry.getKey().indexOf("{") > -1 || entry.getKey().indexOf("}") > -1) {
                    continue;
                }
                if (entry.getKey().equals("accesToken") || entry.getKey().equals("authToken") || entry.getKey().equals("Tag") || entry.getKey().equals("Dialog")) {
                    continue;
                }
                System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
                sb_url.append("&");
                sb_url.append(entry.getKey());
                sb_url.append("=");
                sb_url.append(entry.getValue());
            }
            if ("".equals(sb_url.toString())) return "";
            return sb_url.toString().substring(1);
        } else {
            return "";
        }

    }

}
